package recommend;

import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class XYChartPlot extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public XYChartPlot(String applicationTitle, String chartTitle, String[] tDataLabel, double[][] tData, String[] vDataLabel, double[][] vData, int plotControl) {
		super(applicationTitle);
        // This will create the dataset 
		XYSeriesCollection dataset = createDataset(tDataLabel, tData, vDataLabel, vData, plotControl);
        // based on the dataset we create the chart
        JFreeChart chart = createChart(dataset, chartTitle);
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        // add it to our application
        this.setContentPane(chartPanel);
	}
	
	private  XYSeriesCollection createDataset(String[] tDataLabel, double[][] training_data, String[] vDataLabel, double[][] validation_data, int plotControl) {
		// create a dataset
		XYSeriesCollection dataset = new XYSeriesCollection();
		
		// create and add series to dataset
		for(int i = 0; i < plotControl; i ++) {
			XYSeries tSeries = new XYSeries(tDataLabel[i]);
			for(int j = 0; j < training_data[i].length; j ++) {
				tSeries.add(j, training_data[i][j]);
			}
			dataset.addSeries(tSeries);
			XYSeries vSeries = new XYSeries(vDataLabel[i]);
			for(int j = 0; j < validation_data[i].length; j ++) {
				vSeries.add(j, validation_data[i][j]);
			}
			dataset.addSeries(vSeries);
		}
		
		return dataset;
	}
	
	private JFreeChart createChart(XYSeriesCollection dataset, String title) {
		// Generate the graph
		JFreeChart chart = ChartFactory.createXYLineChart(title, // Title
				"x-axis", // x-axis Label
				"y-axis", // y-axis Label
				dataset, // Dataset
				PlotOrientation.VERTICAL, // Plot Orientation
				true, // Show Legend
				true, // Use tooltips
				false // Configure chart to generate URLs?
				);
		try {
			ChartUtilities.saveChartAsJPEG(new File("RegSqError.jpg"), chart, 500,
					300);
		} catch (IOException e) {
			System.err.println("Problem occurred creating chart.");
		}
		
		final XYPlot plot = (XYPlot) chart.getPlot();
        final NumberAxis axis = (NumberAxis) plot.getRangeAxis();
        axis.setAutoRangeIncludesZero(false);
        axis.setAutoRangeMinimumSize(1.0);

        return chart;
	}
	
//	public static void main(String[] args) {
//        final XYChartPlot plot = new XYChartPlot("XY Series Demo", "XY Chart");
//        plot.pack();
//        RefineryUtilities.centerFrameOnScreen(plot);
//        plot.setVisible(true);
//	}
}
